package com.micro.ai.template.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.micro.ai.template.entity.TemplateStatistic;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 模板统计Mapper
 *
 * @author micro-ai
 * @since 0.0.1
 */
@Mapper
public interface TemplateStatisticMapper extends BaseMapper<TemplateStatistic> {

    /**
     * 根据模板ID和日期范围查询统计数据
     */
    @Select("SELECT * FROM template_statistics WHERE template_id = #{templateId} " +
            "AND stat_date >= #{startDate} AND stat_date <= #{endDate} ORDER BY stat_date DESC")
    List<TemplateStatistic> selectByDateRange(
            @Param("templateId") String templateId,
            @Param("startDate") LocalDate startDate,
            @Param("endDate") LocalDate endDate
    );

    /**
     * 查询热门模板（按使用次数）
     */
    @Select("SELECT template_id, SUM(use_count) as total_use_count FROM template_statistics " +
            "WHERE stat_date >= #{startDate} GROUP BY template_id ORDER BY total_use_count DESC LIMIT #{limit}")
    List<TemplateStatistic> selectHotTemplates(@Param("startDate") LocalDate startDate, @Param("limit") Integer limit);
}

